package com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.service.impl;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.dbmodel.utils.DataSourceUtil;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customer.erp_model.eRP_SalesOutBand.entity.ErpSalesoutbandenrty;
import com.xjrsoft.module.customer.erp_model.eRP_SalesOutBand.service.IErpSalesoutbandentryService;
import com.xjrsoft.module.customer.erp_model.eRP_SalesOutBand.vo.ErpSalesoutbandenrtyVo;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.dto.ErpSalesreturnsDto;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.dto.ErpSalesreturnsentryDto;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.entity.ErpSalesreturns;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.entity.ErpSalesreturnsentry;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.mapper.ErpSalesreturnsentryMapper;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.dto.ErpSalesreturnsListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.mapper.ErpSalesreturnsMapper;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.service.IErpSalesreturnsService;
import com.xjrsoft.core.mp.base.BaseService;
import com.xjrsoft.module.customer.erp_model.eRP_SalesReturns.vo.ErpSalesreturnsentryVo;
import com.xjrsoft.module.customer.erp_model.erp_inventoryManage.entity.ErpInventory;
import com.xjrsoft.module.customer.erp_model.erp_inventoryManage.service.IErpInventoryService;
import com.xjrsoft.module.customer.erp_model.salesOrder.entity.ErpSalesorder;
import com.xjrsoft.module.customer.erp_model.salesOrder.entity.ErpSalesorderentry;
import com.xjrsoft.module.customer.erp_model.salesOrder.service.IErpSalesorderService;
import com.xjrsoft.module.customer.erp_model.salesOrder.service.IErpSalesorderentryService;

import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 *  服务实现类
 *
 * @author pp
 * @since 2021-10-08
 */
@Service
@AllArgsConstructor
public class ErpSalesreturnsServiceImpl extends BaseService<ErpSalesreturnsMapper, ErpSalesreturns> implements IErpSalesreturnsService {

	private ErpSalesreturnsentryMapper erpSalesreturnsentryMapper;
	@Autowired
	private IErpInventoryService erpInventoryService;
	@Autowired
	private IErpSalesorderentryService erpSalesorderentryService;

	private IErpSalesoutbandentryService erpSalesoutbandentryService;

	@Autowired
	private IErpSalesorderService erpSalesorderService;

	@Override
	public IPage<ErpSalesreturns> getPageList(ErpSalesreturnsListDto pageListDto) {
		Wrapper<ErpSalesreturns> wrapper = Wrappers.<ErpSalesreturns>query().lambda()				.ge(!StringUtil.isEmpty(pageListDto.getF_ReturnsDate_Start()), ErpSalesreturns::getFReturnsdate, pageListDto.getF_ReturnsDate_Start())
				.le(!StringUtil.isEmpty(pageListDto.getF_ReturnsDate_End()), ErpSalesreturns::getFReturnsdate, pageListDto.getF_ReturnsDate_End())
				.eq(!StringUtil.isEmpty(pageListDto.getF_SalesOutBandId()), ErpSalesreturns::getFSalesoutbandid, pageListDto.getF_SalesOutBandId())
				.eq(!StringUtil.isEmpty(pageListDto.getF_SalesReturnCode()), ErpSalesreturns::getFSalesreturncode, pageListDto.getF_SalesReturnCode());
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}
	@SneakyThrows
	@Override
	public String addErpSalesreturns(ErpSalesreturns erpSalesreturns, List<ErpSalesreturnsentry> erpSalesreturnsentryList,ErpSalesreturnsDto erpSalesreturnsDto, List<ErpSalesreturnsentryDto> erpSalesreturnsentryDtos)  {
		updateInfo(erpSalesreturnsDto,erpSalesreturnsentryDtos);
		String erpSalesreturnsId = IdWorker.get32UUID();
		erpSalesreturns.setFId(erpSalesreturnsId);
		erpSalesreturnsentryList.forEach(erpSalesreturnsentry -> erpSalesreturnsentry.setFSalesreturnid(erpSalesreturnsId));
		this.saveBatch(erpSalesreturnsentryList, ErpSalesreturnsentry.class, ErpSalesreturnsentryMapper.class);
		boolean isSuccess = this.save(erpSalesreturns);
		return isSuccess ? erpSalesreturnsId : null;
	}

	@SneakyThrows
	@Override
	public boolean updateErpSalesreturns(String id, ErpSalesreturns erpSalesreturns, List<ErpSalesreturnsentry> erpSalesreturnsentryList,ErpSalesreturnsDto erpSalesreturnsDto, List<ErpSalesreturnsentryDto> erpSalesreturnsentryDtos) {
		updateInfo(erpSalesreturnsDto,erpSalesreturnsentryDtos);
		erpSalesreturns.setFId(id);
		erpSalesreturnsentryList.forEach(erpSalesreturnsentry -> erpSalesreturnsentry.setFSalesreturnid(id));
		erpSalesreturnsentryMapper.delete(Wrappers.<ErpSalesreturnsentry>query().lambda().eq(ErpSalesreturnsentry::getFSalesreturnid, id));
		this.saveBatch(erpSalesreturnsentryList, ErpSalesreturnsentry.class, ErpSalesreturnsentryMapper.class);
		return this.updateById(erpSalesreturns);
	}
	public  boolean updateInfo(ErpSalesreturnsDto erpSalesreturnsDto, List<ErpSalesreturnsentryDto> erpSalesreturnsentryDtos) throws SQLException {
		List<ErpSalesorderentry> erpSalesorderentryList=new ArrayList<ErpSalesorderentry>();
		List<ErpSalesoutbandenrty> erpSalesoutbandenrtyList=new ArrayList<ErpSalesoutbandenrty>();
		ErpSalesorder erpSalesorder=new ErpSalesorder();
		List<Integer> statusList=new ArrayList<Integer>();
		for (int i=0;i<erpSalesreturnsentryDtos.size();i++)
		{
			ErpSalesorderentry erpSalesorderentry=new ErpSalesorderentry();
			ErpSalesoutbandenrty erpSalesoutbandenrty=new ErpSalesoutbandenrty();
			//入库单位和基本单位和库存显示单位都一样可以直接做操作  1.对库存做操作（新增或修改）
			if(erpSalesreturnsentryDtos.get(i).getFBasicunit().equals(erpSalesreturnsentryDtos.get(i).getFUnit())&&erpSalesreturnsentryDtos.get(i).getFComunit().equals(erpSalesreturnsentryDtos.get(i).getFUnit()))
			{
				//根据商品id和仓库id查询是否有此商品的库存数据
				String sql1="SELECT * FROM erp_inventory i where i.F_WarehouseId='"+erpSalesreturnsDto.getFWarehouseid()+ "' and i.F_GoodsId='"+erpSalesreturnsentryDtos.get(i).getFGoodsid()+"'";
				List<Entity> isInventory1= Db.use(DataSourceUtil.getDataSource("localDB")).query(sql1);
				BigDecimal quantity=new BigDecimal(erpSalesreturnsentryDtos.get(i).getFQuantity());
				ErpInventory erpInventory1=new ErpInventory();
				//如果有的话就修改对应商品的库存
				if (isInventory1.size()>0)
				{
					BigDecimal aftBasicQua= isInventory1.get(0).getBigDecimal("f_basicamount");
					BigDecimal aftComamQua=isInventory1.get(0).getBigDecimal("f_comamount");
					erpInventory1.setFId(isInventory1.get(0).get("f_id").toString());//根据id修改 一定呀加这句
					erpInventory1.setFGoodsid(erpSalesreturnsentryDtos.get(i).getFGoodsid());
					erpInventory1.setFWarehouseid(erpSalesreturnsDto.getFWarehouseid());
					erpInventory1.setFBasicamount(aftBasicQua.add(quantity));
					erpInventory1.setFComamount(aftComamQua.add(quantity));
					//怎么在一个serveimp里去调用另外一个里面的方法 一定要注入
					erpInventoryService.updateById(erpInventory1);
					erpSalesorderentry.setFId(erpSalesreturnsentryDtos.get(i).getFOrderentryid());
					erpSalesorderentry.setFGoodsid(erpSalesreturnsentryDtos.get(i).getFGoodsid());
					//未入库数量=旧的未入库数量+退货数量
					erpSalesorderentry.setFNooutqua(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFNooutqua()).add(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFQuantity())).intValue());
					//已入库数量=旧的已入库数量-退货数量
					erpSalesorderentry.setFOutqua(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFOutqua()).subtract(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFQuantity())).intValue());
					erpSalesorderentryList.add(erpSalesorderentry);
					erpSalesoutbandenrty.setFId(erpSalesreturnsentryDtos.get(i).getFOutbandentrtyid());
					erpSalesoutbandenrty.setFOutqua(erpSalesorderentry.getFOutqua());
					erpSalesoutbandenrty.setFGoodsid(erpSalesreturnsentryDtos.get(i).getFGoodsid());
					erpSalesoutbandenrty.setFNooutqua(erpSalesorderentry.getFNooutqua());
					erpSalesoutbandenrtyList.add(erpSalesoutbandenrty);
					if(erpSalesorderentry.getFOutqua()==0)
					{
						statusList.add(0);
					}
				}
				else
				{
					erpInventory1.setFGoodsid(erpSalesreturnsentryDtos.get(i).getFGoodsid());
					erpInventory1.setFWarehouseid(erpSalesreturnsDto.getFWarehouseid());
					erpInventory1.setFBasicamount(quantity);
					erpInventory1.setFComamount(quantity);
					//怎么在一个serveimp里去调用另外一个里面的方法 一定要注入
					erpInventoryService.addErpInventory(erpInventory1);
					erpSalesorderentry.setFId(erpSalesreturnsentryDtos.get(i).getFOrderentryid());
					erpSalesorderentry.setFGoodsid(erpSalesreturnsentryDtos.get(i).getFGoodsid());
					erpSalesorderentry.setFNooutqua(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFNooutqua()).add(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFQuantity())).intValue());
					erpSalesorderentry.setFOutqua(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFOutqua()).subtract(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFQuantity())).intValue());
					erpSalesorderentryList.add(erpSalesorderentry);
					erpSalesoutbandenrty.setFId(erpSalesreturnsentryDtos.get(i).getFOutbandentrtyid());
					erpSalesoutbandenrty.setFOutqua(erpSalesorderentry.getFOutqua());
					erpSalesoutbandenrty.setFGoodsid(erpSalesreturnsentryDtos.get(i).getFGoodsid());
					erpSalesoutbandenrty.setFNooutqua(erpSalesorderentry.getFNooutqua());
					erpSalesoutbandenrtyList.add(erpSalesoutbandenrty);
					if(erpSalesorderentry.getFOutqua()==0)
					{
						statusList.add(0);
					}
				}
			}
			//不一样先查询换算率得到之后转换
			else {
				//这里组合有四种情况  只有同时满足不相等的情况 才需要同时转换
				//基本单位转换率
				BigDecimal baseConverRate = null;
				//显示单位转换率
				BigDecimal comConverRate=null;
				if(!erpSalesreturnsentryDtos.get(i).getFBasicunit().equals(erpSalesreturnsentryDtos.get(i).getFUnit()) &&!erpSalesreturnsentryDtos.get(i).getFComunit().equals(erpSalesreturnsentryDtos.get(i).getFUnit()))
				{
					//查询转换率
					String sql="select c.F_Id,c.F_ConvertRate,c.F_FullName,c.F_ConvertId,c.F_Description from erp_unitconvertchild c left join erp_unitconvert u on u.F_Id=c.F_ConvertId where c.F_ConvertId='"+erpSalesreturnsentryDtos.get(i).getFUnit()+"'";
					List<Entity>  list= Db.use(DataSourceUtil.getDataSource("localDB")).query(sql);
					for (int j=0;j<list.size();j++)
					{
						if (list.get(j).get("f_fullname").toString().equals(erpSalesreturnsentryDtos.get(i).getFComunit().toString()))
						{
							comConverRate=new BigDecimal(list.get(j).get("F_ConvertRate").toString());
						}
						if (list.get(j).get("f_fullname").toString().equals(erpSalesreturnsentryDtos.get(i).getFBasicunit().toString()))
						{
							baseConverRate=new BigDecimal(list.get(j).get("F_ConvertRate").toString());
						}
					}
					//得到转换率之后开始转换
					//根据商品id和仓库id查询是否有此商品的库存数据
					String sql1="SELECT * FROM erp_inventory i where i.F_WarehouseId='"+erpSalesreturnsDto.getFWarehouseid()+ "' and i.F_GoodsId='"+erpSalesreturnsentryDtos.get(i).getFGoodsid()+"'";
					List<Entity> isInventory1=Db.use(DataSourceUtil.getDataSource("localDB")).query(sql1);
					ErpInventory erpInventory1=new ErpInventory();
					BigDecimal receiptQua=new BigDecimal(erpSalesreturnsentryDtos.get(i).getFQuantity());
					//如果有的话就修改对应商品的库存
					if (isInventory1.size()>0)
					{
						//修改数量=原有的数据+本次入库的数量*转换率
						BigDecimal aftBasicQua= isInventory1.get(0).getBigDecimal("f_basicamount");
						BigDecimal aftComamQua=isInventory1.get(0).getBigDecimal("f_comamount");
						erpInventory1.setFId(isInventory1.get(0).get("f_id").toString());//根据id修改 一定呀加这句
						erpInventory1.setFWarehouseid(erpSalesreturnsDto.getFWarehouseid());
						erpInventory1.setFBasicamount(aftBasicQua.add(receiptQua.multiply(baseConverRate)));
						erpInventory1.setFComamount(aftComamQua.add(receiptQua.multiply(comConverRate)));
						erpInventoryService.addErpInventory(erpInventory1);
						erpSalesorderentry.setFId(erpSalesreturnsentryDtos.get(i).getFOrderentryid());
						erpSalesorderentry.setFGoodsid(erpSalesreturnsentryDtos.get(i).getFGoodsid());
						//未入库数量=旧的未入库数量+退货数量
						erpSalesorderentry.setFNooutqua(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFNooutqua()).add(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFQuantity())).intValue());
						//已入库数量=旧的已入库数量-退货数量
						erpSalesorderentry.setFOutqua(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFOutqua()).subtract(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFQuantity())).intValue());
						erpSalesorderentryList.add(erpSalesorderentry);
						erpSalesoutbandenrty.setFId(erpSalesreturnsentryDtos.get(i).getFOutbandentrtyid());
						erpSalesoutbandenrty.setFOutqua(erpSalesorderentry.getFOutqua());
						erpSalesoutbandenrty.setFGoodsid(erpSalesreturnsentryDtos.get(i).getFGoodsid());
						erpSalesoutbandenrty.setFNooutqua(erpSalesorderentry.getFNooutqua());
						erpSalesoutbandenrtyList.add(erpSalesoutbandenrty);
						if(erpSalesorderentry.getFOutqua()==0)
						{
							statusList.add(0);
						}
					}
					else
					{
						erpInventory1.setFWarehouseid(erpSalesreturnsDto.getFWarehouseid());
						erpInventory1.setFBasicamount(receiptQua.multiply(baseConverRate));
						erpInventory1.setFComamount(receiptQua.multiply(comConverRate));
						erpInventoryService.updateById(erpInventory1);
						erpSalesorderentry.setFId(erpSalesreturnsentryDtos.get(i).getFOrderentryid());
						erpSalesorderentry.setFGoodsid(erpSalesreturnsentryDtos.get(i).getFGoodsid());
						//未入库数量=旧的未入库数量+退货数量
						erpSalesorderentry.setFNooutqua(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFNooutqua()).add(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFQuantity())).intValue());
						//已入库数量=旧的已入库数量-退货数量
						erpSalesorderentry.setFOutqua(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFOutqua()).subtract(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFQuantity())).intValue());
						erpSalesorderentryList.add(erpSalesorderentry);
						erpSalesoutbandenrty.setFId(erpSalesreturnsentryDtos.get(i).getFOutbandentrtyid());
						erpSalesoutbandenrty.setFOutqua(erpSalesorderentry.getFOutqua());
						erpSalesoutbandenrty.setFGoodsid(erpSalesreturnsentryDtos.get(i).getFGoodsid());
						erpSalesoutbandenrty.setFNooutqua(erpSalesorderentry.getFNooutqua());
						erpSalesoutbandenrtyList.add(erpSalesoutbandenrty);
						if(erpSalesorderentry.getFOutqua()==0)
						{
							statusList.add(0);
						}
					}
				}
				else {
					//其中一个需要转换 先判断是基本数量单位需要转换还是库存显示单位需要转换
					//如果库存显示单位和入库单位不相等 就需要转换,否则就是基本单位需要转换
					//先查询出转换率
					String sql="select c.F_Id,c.F_ConvertRate,c.F_FullName,c.F_ConvertId,c.F_Description from erp_unitconvertchild c left join erp_unitconvert u on u.F_Id=c.F_ConvertId where c.F_ConvertId='"+erpSalesreturnsentryDtos.get(i).getFUnit()+"'";
					List<Entity>  list= Db.use(DataSourceUtil.getDataSource("localDB")).query(sql);

					for (int j=0;j<list.size();j++)
					{
						if (list.get(j).get("f_fullname").toString().equals(erpSalesreturnsentryDtos.get(i).getFComunit().toString()))
						{
							comConverRate=new BigDecimal(list.get(j).get("F_ConvertRate").toString());
						}
						if (list.get(j).get("f_fullname").toString().equals(erpSalesreturnsentryDtos.get(i).getFBasicunit().toString()))
						{
							baseConverRate=new BigDecimal(list.get(j).get("F_ConvertRate").toString());
						}
					}
					//得到转换率之后开始转换
					//根据商品id和仓库id查询是否有此商品的库存数据
					String sql1="SELECT * FROM erp_inventory i where i.F_WarehouseId='"+erpSalesreturnsDto.getFWarehouseid()+ "' and i.F_GoodsId='"+erpSalesreturnsentryDtos.get(i).getFGoodsid()+"'";
					List<Entity> isInventory1=Db.use(DataSourceUtil.getDataSource("localDB")).query(sql1);
					ErpInventory erpInventory1=new ErpInventory();
					//本次出库数量
					BigDecimal receiptQua=new BigDecimal(erpSalesreturnsentryDtos.get(i).getFQuantity());
					//修改数量=原有的数据-本次出库的数量*转换率
					//如果有的话就修改对应商品的库存
					if (isInventory1.size()>0)
					{
						BigDecimal aftBasicQua=isInventory1.get(0).getBigDecimal("f_basicamount");
						BigDecimal aftComamQua=isInventory1.get(0).getBigDecimal("f_comamount");
						erpInventory1.setFId(isInventory1.get(0).get("f_id").toString());//根据id修改 一定呀加这句
						erpInventory1.setFGoodsid(erpSalesreturnsentryDtos.get(i).getFGoodsid());
						erpInventory1.setFWarehouseid(erpSalesreturnsDto.getFWarehouseid());
						erpInventory1.setFBasicamount(aftBasicQua.add(receiptQua));
						erpInventory1.setFComamount(aftComamQua.add(receiptQua));
						//是否需要转换 需要的话就重新赋值
						if (!erpSalesreturnsentryDtos.get(i).getFComunit().equals(erpSalesreturnsentryDtos.get(i).getFUnit()))
						{
							erpInventory1.setFComamount(aftComamQua.add(receiptQua.multiply(comConverRate)));
						}
						else
						{
							erpInventory1.setFBasicamount(aftBasicQua.add(receiptQua.multiply(baseConverRate)));
						}
						erpInventoryService.updateById(erpInventory1);
						erpSalesorderentry.setFId(erpSalesreturnsentryDtos.get(i).getFOrderentryid());
						erpSalesorderentry.setFGoodsid(erpSalesreturnsentryDtos.get(i).getFGoodsid());
						//未入库数量=旧的未入库数量+退货数量
						erpSalesorderentry.setFNooutqua(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFNooutqua()).add(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFQuantity())).intValue());
						//已入库数量=旧的已入库数量-退货数量
						erpSalesorderentry.setFOutqua(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFOutqua()).subtract(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFQuantity())).intValue());
						erpSalesorderentryList.add(erpSalesorderentry);
						erpSalesoutbandenrty.setFId(erpSalesreturnsentryDtos.get(i).getFOutbandentrtyid());
						erpSalesoutbandenrty.setFOutqua(erpSalesorderentry.getFOutqua());
						erpSalesoutbandenrty.setFGoodsid(erpSalesreturnsentryDtos.get(i).getFGoodsid());
						erpSalesoutbandenrty.setFNooutqua(erpSalesorderentry.getFNooutqua());
						erpSalesoutbandenrtyList.add(erpSalesoutbandenrty);
						if(erpSalesorderentry.getFOutqua()==0)
						{
							statusList.add(0);
						}
					}
					else
					{
						erpInventory1.setFGoodsid(erpSalesreturnsentryDtos.get(i).getFGoodsid());
						erpInventory1.setFWarehouseid(erpSalesreturnsDto.getFWarehouseid());
						erpInventory1.setFBasicamount(receiptQua);
						erpInventory1.setFComamount(receiptQua);
						//是否需要转换 需要的话就重新赋值
						if (!erpSalesreturnsentryDtos.get(i).getFComunit().equals(erpSalesreturnsentryDtos.get(i).getFUnit()))
						{
							erpInventory1.setFComamount(receiptQua.multiply(comConverRate));
						}
						else
						{
							erpInventory1.setFBasicamount(receiptQua.multiply(baseConverRate));
						}
						erpInventoryService.addErpInventory(erpInventory1);
						erpSalesorderentry.setFId(erpSalesreturnsentryDtos.get(i).getFOrderentryid());
						erpSalesorderentry.setFGoodsid(erpSalesreturnsentryDtos.get(i).getFGoodsid());
						//未入库数量=旧的未入库数量+退货数量
						erpSalesorderentry.setFNooutqua(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFNooutqua()).add(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFQuantity())).intValue());
						//已入库数量=旧的已入库数量-退货数量
						erpSalesorderentry.setFOutqua(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFOutqua()).subtract(new BigDecimal(erpSalesreturnsentryDtos.get(i).getFQuantity())).intValue());
						erpSalesorderentryList.add(erpSalesorderentry);
						erpSalesoutbandenrty.setFId(erpSalesreturnsentryDtos.get(i).getFOutbandentrtyid());
						erpSalesoutbandenrty.setFOutqua(erpSalesorderentry.getFOutqua());
						erpSalesoutbandenrty.setFGoodsid(erpSalesreturnsentryDtos.get(i).getFGoodsid());
						erpSalesoutbandenrty.setFNooutqua(erpSalesorderentry.getFNooutqua());
						erpSalesoutbandenrtyList.add(erpSalesoutbandenrty);
						if(erpSalesorderentry.getFOutqua()==0)
						{
							statusList.add(0);
						}
					}
				}
			}
		}
		//2.对销售订单的商品数量做修改（已出库数量、未出库数量）
		erpSalesorder.setFId(erpSalesreturnsDto.getFSalesorderid());
		if (statusList.size()==erpSalesreturnsentryDtos.size())
		{
			erpSalesorder.setFStatus(1);
		}
		else
		{
			erpSalesorder.setFStatus(0);
		}
		erpSalesorderService.updateById(erpSalesorder);
		erpSalesorderentryService.updateBatchById(erpSalesorderentryList);
		erpSalesoutbandentryService.updateBatchById(erpSalesoutbandenrtyList);
		return  true;
	}
	public List<ErpSalesreturnsentry> getErpSalesreturnsentryByParentId(String parentId){
		Wrapper wrapper = Wrappers.<ErpSalesreturnsentry>query().lambda().eq(ErpSalesreturnsentry::getFSalesreturnid, parentId);
		return erpSalesreturnsentryMapper.selectList(wrapper);
	}

	public List<ErpSalesreturnsentryVo> getEntryInfo(String F_SalesOutId)
	{
		return erpSalesreturnsentryMapper.getEntryInfo(F_SalesOutId);
	}
}
